package ar.com.photo_admin.domain

import grails.converters.*
import org.springframework.dao.DataIntegrityViolationException

import grails.plugins.springsecurity.Secured

@Secured(["isAuthenticated()"])
class EventAlbumController {

    static allowedMethods = [save: "POST"]

	def springSecurityService
	
	def eventAlbumService
	
	def eventService
	
    def index() {
        redirect(action: "list", params: params)
    }

	@Secured(['ROLE_PHOTOGRAPHER', 'ROLE_ADMIN', 'ROLE_CLIENT'])
    def create() {
		User user = springSecurityService.currentUser
		
		Event event = Event.allEvents(user).get(params.eventId)
		
		if (!event) {
			flash.error = message(code: 'event.no.exist')
			redirect(controller: "event", action: "list")
			return
		} 
		
		def eventAlbumInstance = new EventAlbum(params)
		eventAlbumInstance.event = event
		render(view: "create", model: [eventAlbumInstance: eventAlbumInstance, selectedPhotos : [], unselectedPhotos : eventService.getEventPhotosAsJSON(event), create: true])
	}

	@Secured(['ROLE_PHOTOGRAPHER', 'ROLE_ADMIN', 'ROLE_CLIENT'])
    def save(Long id) {
		User user = springSecurityService.currentUser
		
		Event event = Event.allEvents(user).get(params.eventId)
		
		if (!event) {
			flash.error = message(code: 'eventAlbum.no.exist')
			redirect(controller: "event", action: "list")
			return
		}	
		
		EventAlbum eventAlbumInstance = id != null ? eventAlbumService.getAndCheckOwnership(id) : new EventAlbum(params)
														
		def photosList = []
		
		if (params.selected_photos_id != null && params.selected_photos_id != ""){
			def selectedPhotos =  (params.selected_photos_id).split(",")
			selectedPhotos.each {
				photosList.add(it)
			}
		}
		
		eventAlbumInstance.properties = params
				
		boolean savedCorrectly = eventAlbumService.saveAlbum(eventAlbumInstance, params.cover_photo_id, photosList, event)
		
		if (!savedCorrectly) {
			render(view: "show", model: [eventAlbumInstance: eventAlbumInstance])
			return
		}
		 		
		flash.message = message(code: 'eventAlbum.saved')
		redirect(action: "show", id: eventAlbumInstance.id)
    }

    def show(Long id) {
		User user = springSecurityService.currentUser
		
		EventAlbum eventAlbumInstance = EventAlbum.get(id)
		
        if (!user.isAllowed(eventAlbumInstance)) {
            flash.error = message(code: 'eventAlbum.no.exist')
            redirect(controller: "event", action: "list")
			return
        }
		
		[eventAlbumInstance: eventAlbumInstance, selectedPhotos: eventAlbumService.getAlbumPhotosAsJSON(eventAlbumInstance)]
    }
	
	@Secured(['ROLE_PHOTOGRAPHER', 'ROLE_ADMIN', 'ROLE_CLIENT'])
	def edit(Long id) {
		User user = springSecurityService.currentUser
		
		EventAlbum eventAlbumInstance = EventAlbum.get(id)
		
		if (!user.isOwner(eventAlbumInstance)) {
			flash.error = message(code: 'eventAlbum.no.exist')
			redirect(controller: "event", action: "list")
			return
		} 
			
		def selectedPhotos = eventAlbumService.getAlbumPhotosAsJSON(eventAlbumInstance)
		def unselectedPhotos = eventAlbumService.getNotInAlbumButPossiblePhotosAsJSON(eventAlbumInstance)
		
		render(view: "create", model: [eventAlbumInstance: eventAlbumInstance, selectedPhotos : selectedPhotos, unselectedPhotos : unselectedPhotos, create: false])
	}
	
	@Secured(['ROLE_PHOTOGRAPHER', 'ROLE_ADMIN', 'ROLE_CLIENT'])
	def cancel(Long id) {
		flash.message = message(code: 'discardChanges.succes')
		redirect(controller:"event", action: "show", id: id)
	}
}
